<?php

namespace app\infrastructure\model;

use app\infrastructure\helper\ResponseHelper;
use think\facade\Validate;
use think\Model;

class BaseModel extends Model
{
    use BaseTrait;
	use InsertTrait;
	use UpdateTrait;
	use QueryTrait;

	/**
	 * 彻底删除操作
	 *
	 * @param string|array $where
	 *
	 * @return bool
	 * @throws \think\db\exception\DbException
	 */
    public function remove($where = ''): bool
    {
        $wheres = empty($where) ? self::wheres() : $where;
        if (empty($wheres)) return false;
        // 数据查询
        $query = $this->db();
        // 批量删除
        if (is_array($wheres)){
            $keys = array_keys($wheres);
            if ($keys['0'] === 0){
                foreach ($wheres as $val){
                    // 主键查询
                    if (is_numeric($val)){
                        $rows = $query->where($this->pk, $val)->delete();
                    }else{
                        $rows = $query->where($val)->delete();
                    }
                    if (empty($rows)){
                        return false;
                    }
                }
            }else{
                $rows = $query->where($wheres)->delete();
            }
        }elseif (is_numeric($wheres)){
            // 主键查询
            $rows = $query->where($this->pk, $wheres)->delete();
        }else{
            $rows = $query->where($wheres)->delete();
        }
        if (empty($rows)){
            return false;
        }
        return true;
    }

    // 数据查询
    public function query($where = '', $field = '', $order = '', $whereOr = '')
    {
        $wheres = empty($where) ? self::wheres() : $where;
        $fields = empty($field) ? self::fields() : $field;
        $orders = empty($order) ? self::orders() : $order;
        $whereOrs = empty($whereOr) ? self::whereOrs() : $whereOr;
        $withs = self::withs();
        $whereRaws = self::whereRaws();
        // 数据查询
        $query = $this->db();
        // 存在withJoin查询
        if (!empty($withs)){
            if (is_array($withs)){
                if (count($withs) != count($withs, 1)){
                    $query->withJoin($withs['0'], $withs['1']);
                }else{
                    $query->with($withs);
                }
            }
        }
        // 是否查询加锁
        if (self::$locks){
            $query = $query->lock(self::$locks);
        }
        // 存在where查询
        if (!empty($wheres)){
            // 主键查询
            if (is_numeric($wheres)){
                $query = $query->where($this->pk, $wheres);
            }else{
                $query = $query->where($wheres);
            }
        }
        // 存在whereRaw查询
        if (!empty($whereRaws)){
            if (is_array($whereRaws)){
                foreach ($whereRaws as $val){
                    $query = $query->whereRaw($val);
                }
            }else{
                $query = $query->whereRaw($whereRaws);
            }
        }
        // 存在whereOr查询
        if (!empty($whereOrs)){
            $query = $query->where($whereOrs);
        }
        // 存在order排序
        if (!empty($orders)){
            $query = $query->order($orders);
        }
        // 指定字段查询
        if (!empty($fields)){
            // 排除查询
            $except = false;
            if (is_array($fields)){
                $except = end($fields);
                if ($except === true){
                    $fields = reset($fields);
                }else{
                    $fields = implode(',', $fields);
                }
            }
            if ($except === true){
                $query->withoutField($fields);
            }else{
                $query->field($fields);
            }
        }
        // 重置查询参数
        self::resetQuery();
        // 返回查询语句
        return $query;
    }

}